package cn.gdcp.democ;

import android.Manifest;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements DelScoreListen {
private Button queryscore;
private MyAdapter myAdapter;
private ListView score_list;
private ArrayList<Score> scoreArrayList=new ArrayList<>();
private Button add_student_score;
private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("分数列表");
        initBundler();
        add_student_scoreEvent();
        queryscoreEvent();
        score_listEvent();
        readScoreDateFromDB();
    }




    private void score_listEvent() {
               score_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                   @Override
                   public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                       Score score=scoreArrayList.get(i);
                       Intent intent=new Intent();
                       intent.setClass(MainActivity.this,EditScoreActivity.class);
                       String stuno=score.getStuno();
                       String name=score.getName();
                       int english=score.getEnglish();
                       int android=score.getAndroid();
                       intent.putExtra("NAME",name);
                       intent.putExtra("STUNO",stuno);
                       intent.putExtra("ENGLISH",english);
                       intent.putExtra("ANDROID",android);
                       startActivityForResult(intent,3000);
                   }
               });
    }


    /*     查询学生按钮的点击事件    */
    private void queryscoreEvent() {
           queryscore.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View view) {
                     readScoreDateFromDB();
               }
           });

    }
                                    /* 读取学生成绩表信息的方法封装   */
    private void readScoreDateFromDB() {
        scoreArrayList.clear();
                        String name;
                        int  english;
                        int  android;
                        Uri uri=Uri.parse("content://com.sql/score");
                        Uri uri1=Uri.parse("content://com.sql/student");
                       Cursor cursor=getContentResolver().query(uri,null,null,null,null);
                       if (cursor!=null){
                           for (cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
                               String stuno=cursor.getString(0);
                               String where="stuno="+stuno;
                               Cursor cursor1=getContentResolver().query(uri1,null,where,null,null);
                               if (cursor1!=null){
                                   for (cursor1.moveToFirst();!cursor1.isAfterLast();cursor1.moveToNext()){
                                       name=cursor1.getString(1);
                                       android=cursor.getInt(1);
                                       english=cursor.getInt(2);
                                           Score score = new Score(stuno, name, android, english);
                                           scoreArrayList.add(score);

                                   }
                               }

                           }
                       }
                       myAdapter.notifyDataSetChanged();
    }


    /*
    添加学生成绩的保存事件
    * */
    private void add_student_scoreEvent() {
                       add_student_score.setOnClickListener(new View.OnClickListener() {
                           @Override
                           public void onClick(View view) {
                               Intent intent=new Intent();
                               intent.setClass(MainActivity.this,AddScoreActivity.class);
                               startActivityForResult(intent,2000);
                           }
                       });
    }
     /*     接收来自 Intent 返回的数据*/
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode==2001){
            String stuno=data.getStringExtra("STUNO");
            int android=data.getIntExtra("ANDROID",0);
            int english=data.getIntExtra("ENGLISH",0);
            Score score=new Score(android,english,stuno);
            saveScoreToDB(score);
            readScoreDateFromDB();
        }
        else if (resultCode==3001){
            String stuno=data.getStringExtra("STUNO");
            String name=data.getStringExtra("NAME");
            int english=data.getIntExtra("ENGLISH",0);
            int android=data.getIntExtra("ANDROID",0);
            Score score=new Score(stuno,name,android,english);
            upScoreToDB(score);
            readScoreDateFromDB();
        }
    }


    /*     更新学生成绩数据到数据库的方法封装；
    * */

    private void upScoreToDB(Score score) {
                Uri uri=Uri.parse("content://com.sql/score");
                ContentValues values=new ContentValues();
                values.put("stuno",score.getStuno());
                values.put("english",score.getEnglish());
                values.put("android",score.getAndroid());
                String where="stuno="+score.getStuno();
                getContentResolver().update(uri,values,where,null);
    }

    /*
        插入学生成绩到数据库的方法封装
    * */
    private void saveScoreToDB(Score score) {
        Uri uri=Uri.parse("content://com.sql/score");
        ContentValues values=new ContentValues();
        values.put("stuno",score.getStuno());
        values.put("android",score.getAndroid());
        values.put("english",score.getEnglish());
        getContentResolver().insert(uri,values);
    }

    /*   初始化捆件      */
    private void initBundler() {
               score_list=findViewById(R.id.list_score);
               queryscore=findViewById(R.id.query_student_score);
               myAdapter=new MyAdapter(scoreArrayList,MainActivity.this, MainActivity.this);
               score_list.setAdapter(myAdapter);
               add_student_score=findViewById(R.id.add_student_score);
    }

    /*     删除学生成绩方法封装    */
    @Override
    public void del_Score(Score score) {
        Log.e(TAG, "del_Score: "+score.getStuno() );
                  Uri uri=Uri.parse("content://com.sql/score");
                  String where = "stuno="+score.getStuno();
                   getContentResolver().delete(uri,where,null);
                   readScoreDateFromDB();
    }




}
